package com.mobile.collect.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;

import com.mobile.collect.R;
import com.mobile.collect.beans.SpinnerItem;
import com.mobile.collect.dialogs.BottomListDialog;
import com.mobile.collect.interfaces.ColumnRequestListener;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import cn.droidlover.xdroidmvp.kit.Kits;
import cn.droidlover.xdroidmvp.kit.NToast;


/**
 * 作者：李学伟
 * 日期：2018/8/9
 * 用途：
 */

public class MySpinner extends BaseView implements BottomListDialog.BottomListSelectListener {

    String hadSelectItem;
    List<String> dicList = new ArrayList<>();
    List<SpinnerItem> spinnerItems;
    ClickItemRealtionListener clickItemRealtionListener;
    ColumnRequestListener columnRequestListener;

    public MySpinner(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                clickView();
            }
        });
    }

    public void clickView() {
        boolean allowSelect = true;
        if (!TextUtils.isEmpty(parentParamer)) {
            allowSelect = columnRequestListener.checkParentData(parentParamer);
        }
        if (allowSelect) {
            showBottomDialog();
        }
    }

    public MySpinner(Context context, ClickItemRealtionListener listener, ColumnRequestListener columnRequestListener) {
        super(context);
        this.clickItemRealtionListener = listener;
        this.columnRequestListener = columnRequestListener;
        mView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                clickView();
            }
        });
    }

    public int getViewResourceId() {
        if (orientation == 0) {
            return R.layout.newspinner;
        }else{
            return R.layout.newspinner_h;
        }
    }

    public void init(String leabelTxt, String hadSelectItem, List<String> dicList) {

        Iterator<String> iterator = dicList.iterator();
        while (iterator.hasNext()) {
            String value = iterator.next();
            if (TextUtils.isEmpty(value)) {
                iterator.remove();
            }
        }
        this.dicList.addAll(dicList);
        this.hadSelectItem = hadSelectItem;
        labelTv.setText(leabelTxt);
        if (TextUtils.isEmpty(hadSelectItem)) {
            if (dicList.size() == 0) {
                valueTv.setHint("点击选择");
            } else {
                valueTv.setText(dicList.get(0));
            }

        } else {
            valueTv.setText(hadSelectItem);
        }
    }

    public void initString(List<String> list) {
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            String item = iterator.next();
            if (TextUtils.isEmpty(item)) {
                iterator.remove();
            } else {
                dicList.add(item);
            }
        }
        if (TextUtils.isEmpty(defaultValue) && list.size() > 0) {
            valueTv.setText(list.get(0));
        }
        this.dicList.addAll(list);
    }

    public void init(List<SpinnerItem> spinnerItems) {
        this.spinnerItems = spinnerItems;
        Iterator<SpinnerItem> iterator = spinnerItems.iterator();
        while (iterator.hasNext()) {
            SpinnerItem item = iterator.next();
            String value = item.getValue();
            if (TextUtils.isEmpty(value)) {
                iterator.remove();
            } else {
                dicList.add(value);
            }
        }
        if (TextUtils.isEmpty(defaultValue) && dicList.size() > 0) {
            valueTv.setText(dicList.get(0));
        }
        valueTv.setHint("点击选择" + label);
    }

    @Override
    public void itemClickCallback(String selectStr) {
        valueTv.setText(selectStr);
        hadSelectItem = selectStr;
        SpinnerItem selectItem = null;
        if (!Kits.Empty.check(spinnerItems)) {
            for (SpinnerItem spinnerItem : spinnerItems) {
                if (spinnerItem.getValue().equals(selectStr)) {
                    selectItem = spinnerItem;
                    continue;
                }
            }
        }
        if (null != clickItemRealtionListener) {
            clickItemRealtionListener.dealRealtionColumn(paramer, selectStr);
        }
    }

    protected void showBottomDialog() {
        if (dicList.size() == 0) {
            NToast.shortToast(mContext, "不存在选择项，请联系管理员");
        } else {
            BottomListDialog bottomListDialog = new BottomListDialog(mContext, this);
            bottomListDialog.disPlay(dicList);
        }
    }

    @Override
    public void reLoad(List<String> values) {
        super.reLoad();
        this.dicList.clear();
        if (null != values) {
            dicList.addAll(values);
        }
        if (values.size()>0) {
            valueTv.setText(values.get(0));
        }
    }



    public void setClickItemRealtionListener(ClickItemRealtionListener clickItemRealtionListener) {
        this.clickItemRealtionListener = clickItemRealtionListener;
    }

    public interface ClickItemRealtionListener {
        void dealRealtionColumn(String paramer, String selectStr);
    }

    public void setColumnRequestListener(ColumnRequestListener columnRequestListener) {
        this.columnRequestListener = columnRequestListener;
    }
}
